package org.dromara.wmscloud.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.wmscloud.domain.vo.BaseWorkshopVo;
import org.dromara.wmscloud.domain.bo.BaseWorkshopBo;
import org.dromara.wmscloud.service.IBaseWorkshopService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 车间
 *
 * @author hou
 * @date 2025-04-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wmsCloud/workshop")
public class BaseWorkshopController extends BaseController {

    private final IBaseWorkshopService baseWorkshopService;

    /**
     * 查询车间列表
     */
    @SaCheckPermission("base:workshop:list")
    @GetMapping("/list")
    public TableDataInfo<BaseWorkshopVo> list(BaseWorkshopBo bo, PageQuery pageQuery) {
        return baseWorkshopService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出车间列表
     */
    @SaCheckPermission("base:workshop:export")
    @Log(title = "车间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseWorkshopBo bo, HttpServletResponse response) {
        List<BaseWorkshopVo> list = baseWorkshopService.queryList(bo);
        ExcelUtil.exportExcel(list, "车间", BaseWorkshopVo.class, response);
    }

    /**
     * 获取车间详细信息
     *
     * @param workshopId 主键
     */
    @SaCheckPermission("base:workshop:query")
    @GetMapping("/{workshopId}")
    public R<BaseWorkshopVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long workshopId) {
        return R.ok(baseWorkshopService.queryById(workshopId));
    }

    /**
     * 新增车间
     */
    @SaCheckPermission("base:workshop:add")
    @Log(title = "车间", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseWorkshopBo bo) {
        return toAjax(baseWorkshopService.insertByBo(bo));
    }

    /**
     * 修改车间
     */
    @SaCheckPermission("base:workshop:edit")
    @Log(title = "车间", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseWorkshopBo bo) {
        return toAjax(baseWorkshopService.updateByBo(bo));
    }

    /**
     * 删除车间
     *
     * @param workshopIds 主键串
     */
    @SaCheckPermission("base:workshop:remove")
    @Log(title = "车间", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workshopIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] workshopIds) {
        return toAjax(baseWorkshopService.deleteWithValidByIds(List.of(workshopIds), true));
    }
}
